<?php


namespace app\manage\controller;

use think\Db;
use think\Log;

class MobilePack extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        // 查询条件start
        $times = input('times', '');
        $title = input('title', '');
        $status = input('status', '');

        $map = [];
        if (!empty($title)) {
            $map['title'] = ['like', "%" . $title . "%"];
        }

        if (!empty($status)) {
            $map['status'] = $status;
        }

        if (!empty($times)) {
            $time_arr = explode(' - ', $times);
            $map['create_time'] = ['between', [strtotime($time_arr[0]), strtotime($time_arr[1])]];
        }

        $infos = model('MobilePack')
            ->where($map)->order('id desc')->paginate();

        return $this->fetch('', [
            'infos' => $infos,
            'title' => $title,
            'times' => $times,
            'status' => $status,
        ]);
    }

    public function add()
    {
        if (request()->isAjax()) {
            try {
                $save_data = input('post.');

                if (empty($save_data['title'])) {
                    return self::showReturnCode(1011, '名称不能为空');
                }
                if (empty($save_data['content'])) {
                    return self::showReturnCode(1011, '手机MD5包不能为空');
                }
                //$content = explode("\n", $save_data['content']);
                //$save_data['content'] = implode(",", $save_data['content']);

                $result = $this->editData(false, '', 'MobilePack', $save_data);
                if ($result['code'] == 1001) {
                    $content = explode("\n", $save_data['content']);
                    $content = array_unique($content);
                    $insert = [];
                    $pack_id = \db('mobile_pack')->getLastInsID();
                    foreach ($content as $v) {
                        if (empty($v)) continue;
                        $insert[] = [
                            'pack_id' => $pack_id,
                            'mobile_md5' => $v,
                        ];
                    }
                    unset($content);
                    \db('mobile_pack_list')->insertAll($insert);
                    $result['msg'] = '新增成功';
                    $this->RecordSystemLog($this->uid, $this->behavior, '手机号MD5包新增成功');
                }

                return $result;
            } catch (\Exception $e) {
                Log::error($e->getMessage());
                return self::showReturnCode(1011, $e->getMessage());
            }
        } else {
            return self::showReturnCode(1002);
        }
    }

    public function getInfo()
    {
        $id = input('id');
        if (!$id) {
            return self::showReturnCode(1002, 'id 错误');
        }
        $info = \db('mobile_pack')->find($id);

        $content = \db('mobile_pack_list')->where('pack_id', $id)->column('mobile_md5');
        $info['content'] = implode("\n", $content);
        return self::showJsonReturnCodeData('1001', $info, '');
    }

    public function edit()
    {
        if (request()->isAjax()) {
            try {
                $save_data = input('post.');
                if (empty($save_data['id'])) {
                    return self::showReturnCode(1011, 'id有误');
                }
                if (empty($save_data['title'])) {
                    return self::showReturnCode(1011, '名称不能为空');
                }
                if (empty($save_data['content'])) {
                    return self::showReturnCode(1011, '手机MD5包不能为空');
                }
                //$save_data['content'] = explode("\n", $save_data['content']);
                //$save_data['content'] = implode(",", $save_data['content']);

                $result = $this->editData(false, '', 'MobilePack', $save_data);
                if ($result['code'] == 1001) {
                    \db('mobile_pack_list')->where('pack_id', $save_data['id'])->delete();
                    $content = explode("\n", $save_data['content']);
                    $content = array_unique($content);
                    $insert = [];
                    $pack_id = $save_data['id'];
                    foreach ($content as $v) {
                        if (empty($v)) continue;
                        $insert[] = [
                            'pack_id' => $pack_id,
                            'mobile_md5' => $v,
                        ];
                    }
                    unset($content);
                    \db('mobile_pack_list')->insertAll($insert);

                    $result['msg'] = '修改成功';
                    $this->RecordSystemLog($this->uid, $this->behavior, '手机号MD5包修改成功');
                }

                return $result;
            } catch (\Exception $e) {
                Log::error($e->getMessage());
                return self::showReturnCode(1011, $e->getMessage());
            }
        } else {
            return self::showReturnCode(1002);
        }
    }

    public function status()
    {
        if ($this->request->isAjax()) {
            $id = input('post.id');
            $res = \db('mobile_pack')->where(['id' => $id])->update(['status' => input('post.status'), 'update_time' => time()]);
            if ($res) {
                $this->RecordSystemLog($this->uid, $this->behavior, '状态操作成功,操作ID: ' . $id);
                return $this->showReturnCode(1001);
            } else {
                return $this->showReturnCode(1009, '操作失败');
            }
        } else {
            return $this->showReturnCode(1002);
        }
    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = input('post.id');

            $res = \db('mobile_pack')->where('id', $id)->delete();
            if ($res) {
                \db('mobile_pack_list')->where('pack_id', $id)->delete();
                $this->RecordSystemLog($this->uid, $this->behavior, '删除成功,操作ID: ' . $id);
                return $this->showReturnCode(1001);
            } else {
                return $this->showReturnCode(1009, '删除失败');
            }
        } else {
            return $this->showReturnCode(1002);
        }
    }
}